﻿using LoveNote.Application.Share.Enums;
using LoveNote.Application.Share.Login;
using LoveNote.Application.Share.Message;
using LoveNote.Database.Entities;
using LoveNote.Database.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Bindings;
using TookKit.Mvvm.Enums;
using TookKit.Mvvm.Events;

namespace LoveNote.Application.Login.ViewModels
{
    [DependencyInjection(Lifetime.Singleton)]
    internal class LoginViewModel : ObservableObject
    {
        private UserEntity user = new UserEntity();
        /// <summary>
        /// 登录用户
        /// </summary>
        public UserEntity User
        {
            get { return user; }
            set { SetProperty(ref user, value); }
        }
        public ISession Session { get;}

        public ICommand LoadedCommand { get; }
        public ICommand LoginCommand { get;}
        public ICommand PasswordChangedCommand { get; }
        public ICommand OpenWpfsoftCommand { get; }

        private IUserRepository UserRepository { get; }
        public LoginViewModel(ISession session, IUserRepository userRepository)
        {
            Session = session;
            UserRepository = userRepository;

            LoadedCommand = new RelayCommand(OnLoadedCommand);
            LoginCommand = new RelayCommand<PasswordBox>(OnLoginCommand);
            PasswordChangedCommand = new RelayCommand<PasswordBox>(OnPasswordChangedCommand);
            OpenWpfsoftCommand = new RelayCommand(OnOpenWpfsoftCommand);
        }

        private void OnOpenWpfsoftCommand()
        {
            System.Diagnostics.Process.Start("explorer.exe", "https://www.wpfsoft.com");
        }

        private void OnPasswordChangedCommand(PasswordBox passwordbox)
        {
            User.Password = passwordbox.Password;
        }

        private void OnLoadedCommand()
        {
            User = new UserEntity();
        }

        private void OnLoginCommand(PasswordBox passwordbox)
        {
#if DEBUG
            User.UserName = "admin";
            User.Password = "12345678";
#endif
            if (string.IsNullOrEmpty(User.UserName) || string.IsNullOrEmpty(User.Password))
            {
                Session.Show("用户名或密码为空");
                return;
            }

            var user = UserRepository.Search(User.UserName);
            if (user == null)
            {
                user = new Database.Entities.UserEntity();
                user.UserName = User.UserName;
                user.Password = User.Password;
                user.Role = (int)RoleType.超级管理员;
                user.InsertDate = DateTime.Now;
                var count = UserRepository.Insert(user);
                if (count > 0)
                {
                    Session.Show($"注册新用户成功!\r\n用户名:{user.UserName}\r\n密码:{user.Password}");
                }
                Session.CurrentUser = user;
                EventBus.Default.Publish(this, new LoginMessage(user));
            }
            else
            {
                if(user.Password!= User.Password)
                {
                    Session.Show("用户名或密码错误");
                }
                else
                {
                    Session.CurrentUser = user;
                    EventBus.Default.Publish(this, new LoginMessage(user));
                }                
            }

            passwordbox.Password = string.Empty;
        }
    }
}
